import { loginAPI } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() | ''
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      // 设置到Cookies
      setToken(newToken)
    },
    clearUserInfo(state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    async asyncLogin(cxt, { username, password }) {
      // 1. 调用登录接口
      const res = await loginAPI({ username, password })
      // 2. 提交mutation
      cxt.commit('setToken', res.data.token)
    }
  }
}
